package com.roshan.web.util;

import org.springframework.util.StringUtils;

import com.roshan.web.util.constants.CommonConstants;
import com.roshan.web.util.constants.RegexConstants;

public class SecurityUtils {

    public static String removeCarriageReturnAndLineFeed(String input) {
        if (!StringUtils.hasText(input)) {
            return CommonConstants.EMPTY;
        }
        String output = input.replaceAll(RegexConstants.CARRIAGE_RETURN_AND_LINEFEED_RGX, CommonConstants.EMPTY);
        output = removeCRLFWithASCIICodes(output);
        return output;
    }

    /**
     * Removes the crlf with ascii codes.
     * 
     * @param content the content
     * @return the string
     */
    public static String removeCRLFWithASCIICodes(String content) {
        StringBuffer stringBuffer = new StringBuffer();

        char[] charArray = content.toCharArray();

        for (int i = 0; i < charArray.length; i++) {
            if (CommonConstants.LINE_FEED != charArray[i] && CommonConstants.CARRIAGE_RETURN != charArray[i]) {
                stringBuffer.append(charArray[i]);
            }
        }

        return stringBuffer.toString();

    }

}
